/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.plian.system.controller.pf;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.StringUtil;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.common.status.CustomCode;
import com.plian.system.config.Resubmit;
import com.plian.system.dto.pf.BaseBusinessProgressDTO;
import com.plian.system.entity.pf.BaseBusinessProgress;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.search.TokenSearch;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.properties.GzwProperties;
import com.plian.system.service.pf.IBaseBusinessProgressService;
import com.plian.system.service.wf.FormWorkflowService;
import com.plian.system.vo.pf.BaseBusinessProgressVO;
import com.plian.system.wrapper.pf.BaseBusinessProgressWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * 经营业绩考核进度主表 控制器
 *
 * @author
 * @since 2019-09-27
 */
@RestController
@AllArgsConstructor
@RequestMapping("api/basebusinessprogress")
@Api(value = "BaseBusinessProgressController", tags = "经营业绩考核进度主表 控制器")
public class BaseBusinessProgressController extends BaseController {

	private IBaseBusinessProgressService baseBusinessProgressService;

	private FormWorkflowService formWorkflowService;

	private BaseBusinessProgressWrapper baseBusinessProgressWrapper;

	private GzwProperties gzwProperties;

	@PostMapping("/detail")
	@ApiOperation(value = "详情", notes = "传入 baseBusinessProgress")
	public R<BaseBusinessProgressVO> detail(@RequestBody BaseBusinessProgress baseBusinessProgress) {
		baseBusinessProgress.setModuleType(0);
		BaseBusinessProgress detail = baseBusinessProgressService.getOne(Condition.getQueryWrapper(baseBusinessProgress));
		BaseBusinessProgressVO baseBusinessIndicatorsVO = baseBusinessProgressWrapper.entityToVO(detail);
		return baseBusinessProgressService.getDetailObj(baseBusinessIndicatorsVO);
	}

	@PostMapping("/list")
	@ApiOperation(value = "分页", notes = "传入query")
	public ListResult<BaseBusinessProgressVO> list(@RequestBody Query query) {
		List<SearchEntity> entityList = query.getSearch();
		if (query.getAscs()==null&&query.getDescs()==null){
			query.setDescs("create_time");
		}
		QueryWrapper<BaseBusinessProgress> queryWrapper = SearchParamEntiy.getQueryWrapper(
				Condition.getQueryWrapper(new BaseBusinessProgress()), entityList
		);
		queryWrapper.eq("module_type",0);
		TokenSearch<BaseBusinessProgress> tokenSearch = new TokenSearch<>();
		tokenSearch.addTokenQuery(queryWrapper, query);

		IPage<BaseBusinessProgress> pages = baseBusinessProgressService.page(Condition.getPage(query), queryWrapper);
		List<BaseBusinessProgressVO> baseBusinessIndicatorsVOS = baseBusinessProgressWrapper.entityToVO(pages.getRecords());
		ListResult<BaseBusinessProgressVO> listResult = new ListResult<>(CommonCode.SUCCESS);
		listResult.setList(baseBusinessIndicatorsVOS);
		listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
		return listResult;
	}

	@PostMapping("/submit")
	@Resubmit(delaySeconds = 0)
	@ApiOperation(value = "新增或修改", notes = "传入 baseBusinessProgress")
	@LogOperation("经营业绩考核进度-提交")
	public R<BaseBusinessProgress> submit(@Valid @RequestBody BaseBusinessProgress baseBusinessProgress) {
		baseBusinessProgress.setModuleType(0);
		String checkMessage = baseBusinessProgressService.checkYear(baseBusinessProgress);
		if (StringUtil.isNotBlank(checkMessage)){
			return new R(CustomCode.builder().message(checkMessage).code(1999).success(false).build());
		}

		boolean result = baseBusinessProgressService.submit(baseBusinessProgress);
		HashMap<String, Object> processVariable = new HashMap<>();
		formWorkflowService.startProcessAndSimulate(String.valueOf(baseBusinessProgress.getId()),baseBusinessProgress.getFormType(),processVariable);

		return result ? R.data(baseBusinessProgress) : new R(CommonCode.FAIL);
	}

	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入 ids")
	@LogOperation("经营业绩考核进度-删除")
	public R remove(@RequestBody PageData pd) {
		List<String> ids = (List<String>)pd.get("ids");
		return baseBusinessProgressService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
				? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
	}

	@PostMapping("/getDetailByDto")
	@ApiOperation(value = "根据年度和填报时间获取基础表", notes = "传入 ids")
	public R<BaseBusinessProgressVO> getDetailByDto(@RequestBody BaseBusinessProgressDTO dto) {
		dto.setModuleType(0);
		BaseBusinessProgress baseBusinessProgress = new BaseBusinessProgress();
		baseBusinessProgress.setModuleType(0);
		baseBusinessProgress.setFillingUnitId(dto.getFillingUnitId());
		baseBusinessProgress.setFillingMonth(dto.getFillingMonth());
		String checkMessage = baseBusinessProgressService.checkYear(baseBusinessProgress);
		if (StringUtil.isNotBlank(checkMessage)){
			return new R(CustomCode.builder().message(checkMessage).code(1999).success(false).build());
		}
		BaseBusinessProgressVO detailByDto = baseBusinessProgressService.getDetailByDto(dto);
		return R.data(detailByDto);
	}

}
